package GUI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import main.ScreenController;

import GUI.TabPanels.AdminPanel;
import GUI.TabPanels.BookingPanel;
import GUI.TabPanels.CheckInPanel;
import GUI.TabPanels.CheckOutPanel;
import GUI.TabPanels.OccupancyPanel;
import GUI.TabPanels.StatisticsPanel;
import GUI.TabPanels.ViewBillPanel;
import GUI.TabPanels.ViewGuestsPanel;

@SuppressWarnings("serial")
public class AdminScreen extends JPanel{
	private ScreenController sc;
	private JTabbedPane tabs;

	private BookingPanel booking_panel;
	private CheckInPanel check_in_panel;
	private CheckOutPanel check_out_panel;
	private ViewGuestsPanel view_guests_panel;
	private AdminPanel admin_panel;
	private StatisticsPanel stats_panel;
	private OccupancyPanel occu_panel;
	private ViewBillPanel view_bill;
	
	private JButton logout_button;

	public AdminScreen(){
		this.setLayout(new BorderLayout());
		
		sc = ScreenController.getInstance();
		
		logout_button = new JButton("Logout");

		logout_button.addActionListener(new LogoutButton());
		
		this.add(logout_button, BorderLayout.EAST);
		tabs = new JTabbedPane();
		this.add(tabs, BorderLayout.SOUTH);
		tabs.addChangeListener(new TabChanged());

		booking_panel = BookingPanel.getInstance();
		check_in_panel = new CheckInPanel();
		check_out_panel = new CheckOutPanel();
		view_guests_panel = new ViewGuestsPanel();
		admin_panel = new AdminPanel();
		stats_panel = new StatisticsPanel();
		occu_panel = new OccupancyPanel();
		view_bill = new ViewBillPanel();

		tabs.addTab("Booking", booking_panel);
		tabs.addTab("Check-In", check_in_panel);
		tabs.addTab("Check-Out", check_out_panel);
		tabs.addTab("Current Guests", view_guests_panel);
		tabs.addTab("Admin", admin_panel);
		tabs.addTab("Statistics", stats_panel);
		tabs.addTab("Occupancy Rate", occu_panel);
		tabs.addTab("View Bill", view_bill);

		setVisible(false);
		setVisible(true);
	}

	private class TabChanged implements ChangeListener{

		@Override
		public void stateChanged(ChangeEvent e) {			
			tabs.repaint();

			if(tabs.getSelectedComponent() == booking_panel){
				booking_panel.UpdateBookings();
			} else if(tabs.getSelectedComponent() == check_in_panel){
				check_in_panel.UpdateTable();
			} else if(tabs.getSelectedComponent() == check_out_panel){
				check_out_panel.UpdateTable();
			} else if(tabs.getSelectedComponent() == view_guests_panel){
				view_guests_panel.UpdateTable();
			} 
			
			tabs.repaint();
		}

	}
	
	private class LogoutButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			sc.ViewLoginScreen();
		}
	}
}
